package com.bwie.controller;


import com.bwie.bean.Cargo;
import com.bwie.bean.Stu;
import com.bwie.bean.User;
import com.bwie.service.StuService;
import com.bwie.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StuController {

    @Resource
    private StuService stuService;

    @RequestMapping("list.do")
    public String list(
            HttpSession session,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            String danhao,
            String zsta,
            String zends,
            String ssta,
            String sends,
            String psta,
            String pends,
            String cname,
            String fahuodan,
            String zdren,
            String zdrenhao,
            String beizhu,
            String xx,
            Model model){

        User user = (User) session.getAttribute("user");
        Integer uid = user.getUid();

        Map map = new HashMap();
        map.put("danhao",danhao);
        map.put("zsta",zsta);
        map.put("zends",zends);
        map.put("ssta",ssta);
        map.put("sends",sends);
        map.put("psta",psta);
        map.put("pends",pends);
        map.put("cname",cname);
        map.put("fahuodan",fahuodan);
        map.put("zdren",zdren);
        map.put("zdrenhao",zdrenhao);
        map.put("beizhu",beizhu);
        map.put("xx",xx);

        PageHelper.startPage(pageNum,3);
        List list=stuService.list(map);
        PageInfo pageInfo = new PageInfo(list);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }


    @RequestMapping("addto.do")
    public String addto(){
        return "add";
    }

    @RequestMapping("listc.do")
    @ResponseBody
    public Object listc(){
       List<Cargo> cargos= stuService.listc();
       return cargos;
    }

    @RequestMapping("listu.do")
    @ResponseBody
    public Object listu(){
        List<User> users=stuService.listu();
        return users;
    }

    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Stu stu, HttpSession session,
                      @RequestParam("photo") MultipartFile photo){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            stu.setImgs(upload);
        }
       int i= stuService.add(stu);
        System.out.println(stu+"---------------------");
        System.out.println(stu+"---------------------");
        System.out.println(stu+"---------------------");
        System.out.println(stu+"---------------------");
        System.out.println(stu+"---------------------");
        return i;
    }

    @RequestMapping("updaqx.do")
    @ResponseBody
    public Object updaqx(Integer sid){
        int i=stuService.updaqx(sid);
        return i;
    }

    @RequestMapping("zx.do")
    public String zx(HttpSession session){
        session.invalidate();
        return "redirect:index.jsp";
    }
}
























